package com.Sinewecology.SendMessage;

import com.entrogy.dao.MessageMapper;

import java.util.concurrent.*;

/* ���ܣ���ش����б�������������Ϣ�б��У�ȡ��������Ϣ�������̳߳���ɷ��͡�
 * ���ߣ��Ź���
 * ����ʱ�䣺2018-10-12
 * ״̬��
 *
 *
 * */
public class SendMsgThread extends Thread {
	ExecutorService executor;

	private MessageMapper messageMapper;

	/*public SendMsgThread() {
		super();
		executor = new ThreadPoolExecutor(20, 100, 200, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1000));
//		executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
	}*/

	public SendMsgThread(MessageMapper messageMapper) {
		super();
		executor = new ThreadPoolExecutor(20, 100, 200, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1000));
		this.messageMapper = messageMapper;
	}

	@Override
	public void run() {
		super.run();
		while (true) {
			MobileMsgDataBean mMsg = WillSendMessageList.getFromMobileMsgList();
			if (mMsg != null) {
				SendMessageTask task = new SendMessageTask(mMsg, messageMapper);
				executor.execute(task);
				try {
					Thread.sleep(100);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			WechatMsgDataBean wMsg = WillSendMessageList.getFromWechatMsgList();
			if (wMsg != null) {
				SendMessageTask task = new SendMessageTask(wMsg, messageMapper);
				executor.execute(task);
				try {
					Thread.sleep(300);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			if (mMsg == null && wMsg == null) {
				try {
					Thread.sleep(100);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}

	@Override
	public synchronized void start() {
		super.start();
	}
}
